#!/usr/bin/make -f

# Debian rules for custom U-Boot build
#
# Copyright (c) Siemens AG, 2019-2020
#
# Authors:
#  Le Jin <le.jin@siemens.com>
#  Jan Kiszka <jan.kiszka@siemens.com>
#
# This file is subject to the terms and conditions of the MIT License.  See
# COPYING.MIT file in the top-level directory.
#

ifneq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
export CROSS_COMPILE=$(DEB_HOST_GNU_TYPE)-
SET_CROSS_BUILD_TOOLS=CROSS_BUILD_TOOLS=y
endif

WORKDIR=$(shell cd ..; pwd)
PREBUILD_DIR=$(WORKDIR)/prebuild
KEY=$(WORKDIR)/keys/custMpk.pem
TEMPLATE=$(WORKDIR)/keys/x509-sysfw-template.txt

ifeq ($(IOT2050_VARIANT), advanced)
    UBOOT_CONFIG_A53=am65x_iot2050_advanced_a53_defconfig
    SEBOOT_IMG=$(PREBUILD_DIR)/tiboot3_advanced.bin
    SOC_SECURITY = SE
else
    UBOOT_CONFIG_A53=am65x_iot2050_basic_a53_defconfig
    SEBOOT_IMG=$(PREBUILD_DIR)/tiboot3_basic.bin
    SOC_SECURITY = NONE
endif

ifeq ($(SOC_SECURITY), SE)
    PARALLEL_BUILD=-j 1
    UBOOT_SYSFW=$(PREBUILD_DIR)/sysfw.itb_HS
    UBOOT_A53_SPL_IMAGE=tispl.bin_HS
    UBOOT_A53_IMAGE=u-boot.img_HS
    UBOOT_ATF=$(WORKDIR)/bl31.bin_HS
    UBOOT_TEE=$(WORKDIR)/bl32.bin_HS
else
    PARALLEL_BUILD=-j $(shell echo $$(($$(nproc) * 2)))
    UBOOT_SYSFW=$(PREBUILD_DIR)/sysfw.itb
    UBOOT_A53_SPL_IMAGE=tispl.bin
    UBOOT_A53_IMAGE=u-boot.itb
    UBOOT_ATF=$(WORKDIR)/atf/build/k3/iot2050/release/bl31.bin
    UBOOT_TEE=$(WORKDIR)/optee/out/arm-plat-k3/core/tee-pager.bin
endif

override_dh_auto_build:
	# ATF
	$(MAKE) $(PARALLEL_BUILD) -C $(WORKDIR)/atf \
		PLAT=k3 TARGET_BOARD=iot2050 SPD=opteed
	# Optee
	$(MAKE) $(PARALLEL_BUILD) -C $(WORKDIR)/optee \
		PLATFORM=k3-am65x PLATFORM_FLAVOR_iot2050=y CFG_ARM64_core=y \
		CFG_TEE_CORE_LOG_LEVEL=2 ta-targets=ta_arm64

ifeq ($(SOC_SECURITY), SE)
	# Sign ATF
	$(WORKDIR)/git/tools/k3_gen_x509_cert.sh -c 16 \
		-b $(WORKDIR)/atf/build/k3/iot2050/release/bl31.bin \
		-o $(UBOOT_ATF) -l 0x70000000 -k $(KEY) -t $(TEMPLATE)

	# Sign OP-TEE
	$(WORKDIR)/git/tools/k3_gen_x509_cert.sh -c 16 \
		-b $(WORKDIR)/optee/out/arm-plat-k3/core/tee-pager.bin \
		-o $(UBOOT_TEE) -l 0x9e800000 -k $(KEY) -t $(TEMPLATE)
endif
	# A53 core
	$(MAKE) $(PARALLEL_BUILD) CROSS_COMPILE=$(CROSS_COMPILE) $(UBOOT_CONFIG_A53)
	$(MAKE) $(PARALLEL_BUILD) CROSS_COMPILE=$(CROSS_COMPILE) ATF=$(UBOOT_ATF) TEE=$(UBOOT_TEE)

	# SPI flash image
	dd if=/dev/zero ibs=1k count=$(SPI_FLASH_SIZE_KB) | tr "\000" "\377" > $(SPI_FLASH_IMG)
	dd if=$(SEBOOT_IMG) of=$(SPI_FLASH_IMG) seek=0 oflag=seek_bytes conv=notrunc
	dd if=$(UBOOT_A53_SPL_IMAGE) of=$(SPI_FLASH_IMG) seek=512K oflag=seek_bytes conv=notrunc
	dd if=$(UBOOT_A53_IMAGE) of=$(SPI_FLASH_IMG) seek=2560K oflag=seek_bytes conv=notrunc
	# Env
	dd if=/dev/zero ibs=1k count=128 of=$(SPI_FLASH_IMG) seek=6656K oflag=seek_bytes conv=notrunc
	# Env.bak
	dd if=/dev/zero ibs=1k count=128 of=$(SPI_FLASH_IMG) seek=6784K oflag=seek_bytes conv=notrunc
	# SysFW
	dd if=$(UBOOT_SYSFW) of=$(SPI_FLASH_IMG) seek=6912K oflag=seek_bytes conv=notrunc
	# PRU ethernet FW
	dd if=$(PREBUILD_DIR)/am65x-pru0-prueth-fw.elf of=$(SPI_FLASH_IMG) seek=7936K oflag=seek_bytes conv=notrunc
	dd if=$(PREBUILD_DIR)/am65x-pru1-prueth-fw.elf of=$(SPI_FLASH_IMG) seek=8000K oflag=seek_bytes conv=notrunc
	dd if=$(PREBUILD_DIR)/am65x-rtu0-prueth-fw.elf of=$(SPI_FLASH_IMG) seek=8064K oflag=seek_bytes conv=notrunc
	dd if=$(PREBUILD_DIR)/am65x-rtu1-prueth-fw.elf of=$(SPI_FLASH_IMG) seek=8128K oflag=seek_bytes conv=notrunc

	# Tools
	$(MAKE) $(PARALLEL_BUILD) $(SET_CROSS_BUILD_TOOLS) CROSS_COMPILE=$(CROSS_COMPILE) NO_SDL=1 tools-only envtools

override_dh_auto_install:
	mv tools/env/lib.a tools/env/libubootenv.a

override_dh_auto_test:

%:
	CFLAGS= LDFLAGS= dh $@ --parallel
